<?php
declare (strict_types = 1);

namespace app\admin\validate;

use app\business\ProductSpec;
use think\Validate;

class ProductSpecValue extends Validate{

    protected $rule = [
        "spec_id" => "require|number|checkSpecId",
        "name" => "require",
    ];

    protected $message = [
        "spec_id.require" => "必须选择规格",
        "spec_id.number" => "规格ID不正确",
        "name.require" => "规格值不能为空",
    ];

    protected $scene = [
        "get_list" => ["spec_id"],
        "save" => ["spec_id", "name"],
    ];


    public function checkSpecId($value, $rule, $data) {
        if (!$value) {
            return true;
        }

        $isExist = (new ProductSpec())->isExist($value);
        if (!$isExist) {
            return "此规格已不存在";
        }

        return true;
    }

}